import { createStore } from 'vuex'
import { logout, orginfo, perinfo } from '~/api/manager'
import { getStorage, removeStorage, setStorage } from '~/composables/storage'
import { toast } from '~/composables/util'
import { router } from '~/router'
const store = createStore({
  state() {
    return {
      // 用户基础信息
      user: {},
      // 用户信息
      userMessage: {},
      menus: [
        { name: '首页', path: '/home', type: 'home' },
        { name: '活动中心', path: '/activityCenter', type: 'activityCenter' },
        { name: '投智生态', path: '/corporateEcology', type: 'corporateEcology' },
        { name: '云课堂', path: '/live', type: 'live' },
        { name: '赛事中心', path: '/RaceControl', type: 'RaceControl' },
        { name: '人才中心', path: '/talentTraining', type: 'talentTraining' },
        { name: '资讯中心', path: '/journalism', type: 'journalism' },
        { name: '商城', path: '/shop', type: 'shop' },
        { name: '文库', path: '/library', type: 'library' },
        { name: '关于我们', path: '/visualization', type: '' },
        { name: '需求中心', path: '/focalNeed', type: 'focalNeed' },
      ],
      userType: null,
      isBottom: true,
      isShowPage: true,
    }
  },
  mutations: {
    // 记录用户信息
    SET_USERINFO(state, user) {
      state.user = user
    },
    SET_USERTYPE(state, userType) {
      state.userType = userType
    },
    SET_USERMESSAGE(state, userMessage) {
      state.userMessage = userMessage
    },
    SET_BOTTOM(state, Flag) {
      state.isBottom = Flag
    },
    SET_PAGE(state, Flag) {
      state.isShowPage = Flag
    },
  },
  getters: {
    userID: (state) => {
      if (!state.userType) return null
      if (state.userType == 1)
        return state.userMessage.orgId

      else
        return state.userMessage.userId
    },
  },
  actions: {
    // 获取当前登录用户信息
    getinfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        const userType = getStorage('userType')
        const userInfo = getStorage('userInfo')
        const id = userInfo?.id || ''
        if (userType == 2) {
          perinfo(id).then((res) => {
            if (res && res.code == '0000') {
              commit('SET_USERMESSAGE', res.data.result)
              setStorage('userMessage', res.data.result)
              resolve(res.data.result)
            }
          })
        }
        else {
          orginfo(id).then((res) => {
            if (res && res.code == '0000') {
              commit('SET_USERMESSAGE', res.data.result)
              setStorage('userMessage', res.data.result)
              resolve(res.data.result)
            }
          })
        }
      })
    },
    // 退出登录
    logout({ state, dispatch }) {
      logout(state.user).then((res) => {
        if (res && res.code == '0000') {
          toast('退出成功')
          dispatch('clearFn')
          router.push('/login')
        }
      })
    },
    clearFn({ commit }) {
      commit('SET_USERINFO', {})
      commit('SET_USERTYPE', null)
      commit('SET_USERMESSAGE', null)
      removeStorage('userInfo')
      removeStorage('userType')
      removeStorage('userMessage')
      removeStorage('token')
    },
    init({ commit }) {
      const userInfo = getStorage('userInfo')
      const userType = getStorage('userType')
      const userMessage = getStorage('userMessage')
      if (userInfo)
        commit('SET_USERINFO', userInfo)

      if (userType)
        commit('SET_USERTYPE', userType)

      if (userMessage)
        commit('SET_USERMESSAGE', userMessage)
    },
  },
})

export default store
